<?php


namespace app\api\controller;


use app\admin\model\Address;
use app\admin\model\GoodsSkuValue;
use app\admin\model\User;
use app\admin\model\UserPintuanLog;
use app\common\controller\Api;
use app\admin\model\Goods as GoodsModel;
use app\common\model\Config;
use think\Db;
use think\Env;

class Collage extends Api
{
    protected $noNeedLogin = ['collage','specs'];
    protected $noNeedRight = ['*'];


    /**
     * 拼购显示
     *
     */
    public function index()
    {
        $goods = GoodsModel::where(['type' => 2, 'status' => 1])
            ->where('out_time', '<', time())
            ->field('id,image,sell,title,subtitle,service,address,content,out_time,value_id')
            ->select();
        $config = Config::find(19);
        if (!empty($goods)) {
            foreach ($goods as &$v) {
                $image = explode(',', $v['image']);
                $domain = [];
                foreach ($image as $vvv) {
                    $domain[] = Env::get('app.yuming') . $vvv;
                }
                $v['image'] = $domain;
                $v['content'] = get_img_thumb_url($v['content']);
                $v['out_time'] = date('Y-m-d H:i:s', $v['out_time']);
                $v['price'] = $config['value'];
                $v->unsetAppend();
                unset($v);
            }
        }
        $this->success('获取成功', $goods);
    }

    /**
     * 拼购
     *
     */
    public function collage()
    {
        if ($this->request->isPost()) {
            $ress = Address::where(['is_default' => 1, 'user_id' => $this->auth->id])->find();
            $address_id = input('address_id', $ress['id']);     //收货地址
            $address = Address::field('id,name,phone,city,address,is_default')->find($address_id); //地址
            $address->unsetAppend();
            $package = '0.00';       //运费
            $goods = GoodsModel::where(['type' => 2, 'status' => 1])
                ->where('out_time', '<', time())
                ->field('id,image,title,value_id')
                ->select();
            $con = Config::find(19);
            $price = $con['value'];
            if (!empty($goods)) {
                foreach ($goods as &$v) {
                    $image = explode(',', $v['image']);
                    if (isset($image[0])) {
                        $v['image'] = Env::get('app.yuming') . $image[0];
                    }
                    $v->unsetAppend();
                    unset($v);
                }
            }
            $data = ['address' => $address, 'goods' => $goods, 'price' => $price,'package' => $package];
            $this->success('获取成功', $data);
        }
        $this->error('无效请求');

    }

    /**
     * 拼购规格
     *
     */
    public function specs()
    {
        $id = input('id');
        if (empty($id)) {
            $this->error('参数错误，传入商品id');
        }
        $goods = GoodsModel::field('id,image,type,score,title,value_id')
            ->find($id);
        $config = Config::find(19);
        if ($goods['type'] == 2){
            $goods['score'] = $config['value'];
        }
        $value = GoodsSkuValue::where('id', 'in', $goods['value_id'])
            ->field('id,name,sku_id')
            ->select();
        $sku = [];
        foreach ($value as $vv) {
            $sku[$vv['sku_id']]['name'] = $vv['sku']['name'];
            unset($vv['sku']);
            $sku[$vv['sku_id']]['values'][] = $vv;
        }
        $goods['value_id'] = $sku;
        if (!empty($goods)){
            $image = explode(',', $goods['image']);
            if (isset($image[0])) {
                $goods['image'] = Env::get('app.yuming') . $image[0];
            }
        }
        unset($goods['type']);
        $goods->unsetAppend();
        $this->success('获取成功', $goods);

    }

    /**
     *
     * 立即兑换
     */
    public function exchange()
    {
        if ($this->request->isPost()) {
            $ress = Address::where(['is_default' => 1, 'user_id' => $this->auth->id])->find();
            $address_id = input('address_id', $ress['id']);     //收货地址
            $goods_id = input('goods_id');                      //商品
            $sku_id = input('value_id');                        //规格
            $goods = GoodsModel::field('id,title,subtitle,image')->find($goods_id);
            $address = Address::field('id,name,phone,city,address,is_default')->find($address_id); //地址
            $goods->unsetAppend();
            $address->unsetAppend();
            $con = Config::find(19);
            $price = $con['value'];
            $package = '包邮';       //运费
            if ($goods) {
                $image = explode(',', $goods['image']);
                if (isset($image[0])) {
                    $goods['image'] = Env::get('app.yuming') . $image[0];
                }
                $sku = '';
                if ($sku_id) {
                    $sku_value = GoodsSkuValue::where('id', 'in', $sku_id)
                        ->column('name');
                    $sku = implode(',', $sku_value);
                }
            }
            $data = ['address' => $address, 'goods' => $goods, 'sku_value' => $sku, 'package' => $package, 'price' =>$price];
            $this->success('获取成功', $data);
        }
        $this->error('无效请求');
    }


    /**
     *
     * 提交订单
     */
    public function order()
    {
        if ($this->request->isPost()) {
            $goods_id = input('goods_id');
            if (empty($goods_id)) {
                $this->error('请选择商品');
            }
            $address_id = input('address_id');
            if (empty($address_id)) {
                $this->error('请选择地址');
            }
            $value_id = input('value_id', '');   //规格id
            $user = User::find($this->auth->id);
            $goods = GoodsModel::where('id', $goods_id)
                ->field('id,title,subtitle,stock,image,value_id')
                ->find();
            if (!$goods) {
                $this->error('商品错误');
            }
            if (!empty($goods['value_id'])) {
                if (empty($value_id)) {
                    $this->error('请选择商品规格');
                }
            }
            if ($goods['stock'] <= 0) {
                $this->error('商品库存不足');
            }
            $base = Config::find(19);
            $price = $base['value'];  //价格
            if ($user['money'] < $price) {
                return errMsg(400, '您的余额不足,请充值');
            }
            $today_start = strtotime(date('Y-m-d') . " 00:00:00");
            $today_end = strtotime(date('Y-m-d') . " 23:59:59");
            $shop_num = UserPintuanLog::where(['user_id' => $user['id']])
                ->where('createtime', '>=', $today_start)
                ->where('createtime', '<=', $today_end)
                ->count();
            if ($shop_num > 10) {
                $this->error('每日仅可抽奖10次');
            }
            $data = ['goods_price' => $price, 'member_price' => $user['money']];
            $this->success('获取成功', $data);
        }
        $this->error('无效请求');
    }


    /**
     *
     * 拼购商品支付
     */
    public function payment()
    {

    }
}